import type { TASK_MODE, TaskStaticsTypeEnum } from "@/common/constants/other-keys"
import type { Node } from "@vue-flow/core"
import type { PageRequestData, ResponseTableData, TableData } from "../common"
import { getProjectId } from "@/common/utils/cache/local-storage"
import { Position } from "@vue-flow/core"

export const INIT_DATE_COLLECTION = {
  data_nodes_info: [{
    id: "1",
    type: "input",
    data: { label: "仿真推演" },
    position: { x: 250, y: 0 },
    class: "light",
    sourcePosition: Position.Right
  }]
}

export const DEFAULT_TRAIN_BASIC: EditTrainBasicInfo = {
  task_id: undefined,
  task_mode: "Train",
  task_name: undefined,
  task_tag: [],
  task_describe: undefined,
  project_id: getProjectId(),
  sim_config: {
    engine_id: undefined,
    scn_id: undefined,
    max_step: 1,
    speed_up_ratio: 1
  },
  agent_config: {
    red: [],
    blue: []
  },
  train_config: {
    task_config: {
      resources: {
        cpu_num: 1,
        distributed_enable: false,
        gpu_num: 1,
        worker_num: 1
      }
    },
    episode_config: {
      max_episode: 10
    },
    test_config: {
      test_interval: 1,
      test_num: 1
    },
    save_config: {
      training_sample_data: {
        save_enable: false
      },
      decision_model: {
        save_enable: false
      }
    }
  }
}

export const DEFAULT_DEDUCT_BASIC: EditInfrenceBasicInfo = {
  task_id: undefined,
  task_mode: "Inference",
  task_name: undefined,
  task_tag: [],
  task_describe: undefined,
  project_id: getProjectId(),
  sim_config: {
    engine_id: undefined,
    scn_id: undefined,
    max_step: 1,
    speed_up_ratio: 1
  },
  agent_config: {
    red: [],
    blue: []
  },
  inference_config: {
    task_config: {
      resources: {
        cpu_num: 1,
        distributed_enable: false,
        gpu_num: 1,
        worker_num: 1
      }
    },
    episode_config: {
      max_episode: 10
    },
    end_condition_config: []
  },
  data_collections: {
    is_enable: false,
    opportunity: {
      value: 0,
      key_events: [],
      time_range: {
        start_time: '',
        end_time: ''
      }
    },
    entity_list: [],
    event: []
  },
  situation_config: undefined
}

export interface EvalProcessEvaData {
  // 综合能力
  comprehensive_ability: number
  // 感知能力
  perception: {
    // 侦察能力
    detective: number
    // 区域搜索能力
    regional_search: number
    // 综合能力
    ability: number
  }
  // 认知能力
  cognition: {
    // 自主性
    autonomy: number
    // 协同性
    cooperativity: number
    // 综合能力
    ability: number
  }
  // 决策能力
  decision: {
    // 火力支援
    fire_support: number
    // 博弈学习
    game_learning: number
    // 综合能力
    ability: number
  }
  // 行动能力
  action: {
    // 集群分组
    cluster_grouping: number
    // 协同性
    cooperativity: number
    // 综合能力
    ability: number
  }
}

export type fileTypeStr = "sample" | "agent"

export interface FileTableItem {
  file_id: string
  file_name: string
  type: fileTypeStr
  save_flag: boolean
  describe: string
  file_path: string
  associate_id: string
}

// 已选智能体
export interface SelectAgentItem {
  agent_id: string
  agent_name: string
  agent_describe: string
  agent_interval: number
  evolve_enable: boolean
  trained_agent_id?: string
  selectId?: number
  trained_agent_name?: string
  trained_agent_version?: string
  trained_agent_reward?: string
}

export interface EditTrainBasicInfo {
  task_id?: string // 任务id
  task_mode: TASK_MODE // 任务模式
  task_name?: string // 任务名称
  task_tag?: string[] // 标签
  task_describe?: string // 描述
  project_id?: string // 项目id
  sim_config: SimConfig
  agent_config: AgentConfig
  train_config: TrainConfig
}
export interface EditInfrenceBasicInfo {
  task_id?: string // 任务id
  task_mode: TASK_MODE // 任务模式
  task_name?: string // 任务名称
  task_tag?: string[] // 标签
  task_describe?: string // 描述
  task_status?: number // 任务状态
  task_round?: number // 任务轮次
  task_process?: number // 任务进度
  task_end_time?: string
  task_stop_time?: string
  task_start_time?: string
  project_id?: string // 项目id
  sim_config: SimConfig
  agent_config: AgentConfig
  inference_config: InfrenceConfig
  data_collections: DataCollection
  situation_config?: SituationConfig
}
// 态势信息配置
export interface SituationConfig {
  is_enable: boolean
  params: {
    ip: string
    port: string
    vhost: string
    username: string
    password: string
    rmpAddress: string
  }
}
// 仿真配置
export interface SimConfig {
  engine_id?: string
  scn_id?: string
  engineName?: string
  engineVersion?: string
  max_step: number
  speed_up_ratio: number
  engine_name?: string
  sim_engine_os?: string | string[]
  scn_name?: string
}
// 智能体配置
export interface AgentConfig {
  [propName: string]: SelectAgentItem[]
}
// 训练配置
export interface TrainConfig {
  task_config: {
    resources: {
      cpu_num: number
      distributed_enable: boolean
      gpu_num: number
      worker_num: number
    }
  }
  episode_config: {
    max_episode: number
  }
  test_config: {
    test_interval: number
    test_num: number
  }
  save_config: {
    training_sample_data: {
      save_enable: boolean
    }
    decision_model: {
      save_enable: boolean
    }
  }
}
// 训练配置
export interface InfrenceConfig {
  task_config: {
    resources: {
      cpu_num: number
      distributed_enable: boolean
      gpu_num: number
      worker_num: number
    }
  }
  episode_config: {
    max_episode: number
  }
  end_condition_config: []
}

export interface DataCollection {
  is_enable: boolean;
  opportunity: Opportunity;
  entity_list: EntityItem[];
  event: string[];
}

export interface EntityItem {
  name: string;
  data: string[];
  freq: string;
}

export interface Opportunity {
  value: Node[] | number;
  key_events: string[];
  time_range: {
    start_time: string;
    end_time: string;
  }
}

export interface TableItem extends TableData {
  project_id?: string
  task_describe: string
  task_end_time: string
  task_id: string
  task_mode: TASK_MODE
  task_name: string
  task_process: number
  task_round: number
  task_status: TaskStaticsTypeEnum
  total_duration: number[]
  task_start_time?: string
  task_stop_time?: string
  used_worker_num?: number
  remaining_worker_num?: number
}

export interface ViewBasicInfo {
  project_id?: string
  task_id: string
  task_name: string
  task_process: number
  task_round: number
  task_status: TaskStaticsTypeEnum
  total_duration: number[]
  task_start_time?: string
  task_end_time?: string
  task_stop_time?: string
  used_worker_num?: number
  remaining_worker_num?: number
}

export interface ViewInfo extends ViewBasicInfo {
  sim_config: SimConfig
  agent_config: AgentConfig
  train_config: TrainConfig
  data_collections: DataCollection
}

export interface TableRequestData {
  task_name?: string
  task_tag?: string
  task_mode?: TASK_MODE
  project_id?: string
  task_status?: TaskStaticsTypeEnum
}

export interface TrainProcessEvaData {
  [propName: string]: {
    reward: number
    win_rate: number
  }
}

export interface TaskProcessResData {
  round: number
  process_eval: string
  create_time: Date
  single_round_duration: number
  worker_res_info: {
    used_worker_num: number
    remaining_worker_num: number
  }
}

export interface PageTableRequestData extends TableRequestData, PageRequestData { }
export type PageTableResponseData = ApiResponseData<ResponseTableData<TableItem>>
export type FilePageTableResponseData = ApiResponseData<ResponseTableData<FileTableItem>>
export type TaskCreateResponseData = ApiResponseData<{ task_id: string }>
export type TaskInfoResponseData = ApiResponseData<TableItem | ViewInfo>
export type TaskProcessDataResponseData = ApiResponseData<TaskProcessResData[]>
